﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Terraria.Localization;
using Terraria.Utilities;

namespace Terraria.ModLoader
{
	// Token: 0x020001EC RID: 492
	public abstract class ModPrefix
	{
		// Token: 0x06001529 RID: 5417 RVA: 0x00413D24 File Offset: 0x00411F24
		static ModPrefix()
		{
			foreach (object obj in Enum.GetValues(typeof(PrefixCategory)))
			{
				PrefixCategory key = (PrefixCategory)obj;
				ModPrefix.categoryPrefixes[key] = new List<ModPrefix>();
			}
		}

		// Token: 0x0600152A RID: 5418 RVA: 0x00413DAC File Offset: 0x00411FAC
		internal static byte ReservePrefixID()
		{
			if (ModNet.AllowVanillaClients)
			{
				throw new Exception("Adding items breaks vanilla client compatiblity");
			}
			if (ModPrefix.nextPrefix == 0)
			{
				throw new Exception("Prefix ID limit has been broken");
			}
			byte result = ModPrefix.nextPrefix;
			ModPrefix.nextPrefix += 1;
			return result;
		}

		// Token: 0x0600152B RID: 5419 RVA: 0x0001065E File Offset: 0x0000E85E
		public static ModPrefix GetPrefix(byte type)
		{
			if (type < 84 || type >= ModPrefix.PrefixCount)
			{
				return null;
			}
			return ModPrefix.prefixes[(int)(type - 84)];
		}

		// Token: 0x0600152C RID: 5420 RVA: 0x0001067D File Offset: 0x0000E87D
		public static List<ModPrefix> GetPrefixesInCategory(PrefixCategory category)
		{
			return new List<ModPrefix>(ModPrefix.categoryPrefixes[category]);
		}

		// Token: 0x1700018B RID: 395
		// (get) Token: 0x0600152D RID: 5421 RVA: 0x0001068F File Offset: 0x0000E88F
		public static byte PrefixCount
		{
			get
			{
				return ModPrefix.nextPrefix;
			}
		}

		// Token: 0x0600152E RID: 5422 RVA: 0x00010696 File Offset: 0x0000E896
		internal static void ResizeArrays()
		{
			Array.Resize<LocalizedText>(ref Lang.prefix, (int)ModPrefix.nextPrefix);
		}

		// Token: 0x0600152F RID: 5423 RVA: 0x00413DF4 File Offset: 0x00411FF4
		internal static void Unload()
		{
			ModPrefix.prefixes.Clear();
			ModPrefix.nextPrefix = 84;
			foreach (object obj in Enum.GetValues(typeof(PrefixCategory)))
			{
				PrefixCategory key = (PrefixCategory)obj;
				ModPrefix.categoryPrefixes[key].Clear();
			}
		}

		// Token: 0x06001530 RID: 5424 RVA: 0x00413E70 File Offset: 0x00412070
		internal static void Roll(Item item, ref int prefix, int vanillaWeight, params PrefixCategory[] categories)
		{
			WeightedRandom<byte> weightedRandom = new WeightedRandom<byte>();
			Func<ModPrefix, bool> <>9__0;
			foreach (PrefixCategory key in categories)
			{
				IEnumerable<ModPrefix> source = ModPrefix.categoryPrefixes[key];
				Func<ModPrefix, bool> predicate;
				if ((predicate = <>9__0) == null)
				{
					predicate = (<>9__0 = ((ModPrefix x) => x.CanRoll(item)));
				}
				foreach (ModPrefix modPrefix in source.Where(predicate))
				{
					weightedRandom.Add(modPrefix.Type, (double)modPrefix.RollChance(item));
				}
			}
			if (vanillaWeight > 0)
			{
				weightedRandom.Add((byte)prefix, (double)vanillaWeight);
			}
			prefix = (int)weightedRandom.Get();
		}

		// Token: 0x1700018C RID: 396
		// (get) Token: 0x06001531 RID: 5425 RVA: 0x000106A7 File Offset: 0x0000E8A7
		// (set) Token: 0x06001532 RID: 5426 RVA: 0x000106AF File Offset: 0x0000E8AF
		public Mod mod { get; internal set; }

		// Token: 0x1700018D RID: 397
		// (get) Token: 0x06001533 RID: 5427 RVA: 0x000106B8 File Offset: 0x0000E8B8
		// (set) Token: 0x06001534 RID: 5428 RVA: 0x000106C0 File Offset: 0x0000E8C0
		public string Name { get; internal set; }

		// Token: 0x1700018E RID: 398
		// (get) Token: 0x06001535 RID: 5429 RVA: 0x000106C9 File Offset: 0x0000E8C9
		// (set) Token: 0x06001536 RID: 5430 RVA: 0x000106D1 File Offset: 0x0000E8D1
		public byte Type { get; internal set; }

		// Token: 0x1700018F RID: 399
		// (get) Token: 0x06001537 RID: 5431 RVA: 0x000106DA File Offset: 0x0000E8DA
		// (set) Token: 0x06001538 RID: 5432 RVA: 0x000106E2 File Offset: 0x0000E8E2
		public ModTranslation DisplayName { get; internal set; }

		// Token: 0x06001539 RID: 5433 RVA: 0x0000EAB9 File Offset: 0x0000CCB9
		public virtual float RollChance(Item item)
		{
			return 1f;
		}

		// Token: 0x0600153A RID: 5434 RVA: 0x000106EB File Offset: 0x0000E8EB
		public virtual bool CanRoll(Item item)
		{
			return this.RollChance(item) > 0f;
		}

		// Token: 0x17000190 RID: 400
		// (get) Token: 0x0600153B RID: 5435 RVA: 0x000106FB File Offset: 0x0000E8FB
		public virtual PrefixCategory Category
		{
			get
			{
				return PrefixCategory.Custom;
			}
		}

		// Token: 0x0600153C RID: 5436 RVA: 0x000106FE File Offset: 0x0000E8FE
		public virtual bool Autoload(ref string name)
		{
			return this.mod.Properties.Autoload;
		}

		// Token: 0x0600153D RID: 5437 RVA: 0x00010710 File Offset: 0x0000E910
		public virtual void AutoDefaults()
		{
			if (this.DisplayName.IsDefault())
			{
				this.DisplayName.SetDefault(Regex.Replace(this.Name, "([A-Z])", " $1").Trim());
			}
		}

		// Token: 0x0600153E RID: 5438 RVA: 0x00006B58 File Offset: 0x00004D58
		public virtual void SetDefaults()
		{
		}

		// Token: 0x0600153F RID: 5439 RVA: 0x00006B58 File Offset: 0x00004D58
		public virtual void SetStats(ref float damageMult, ref float knockbackMult, ref float useTimeMult, ref float scaleMult, ref float shootSpeedMult, ref float manaMult, ref int critBonus)
		{
		}

		// Token: 0x06001540 RID: 5440 RVA: 0x00006B58 File Offset: 0x00004D58
		public virtual void ValidateItem(Item item, ref bool invalid)
		{
		}

		// Token: 0x06001541 RID: 5441 RVA: 0x00006B58 File Offset: 0x00004D58
		public virtual void Apply(Item item)
		{
		}

		// Token: 0x06001542 RID: 5442 RVA: 0x00006B58 File Offset: 0x00004D58
		public virtual void ModifyValue(ref float valueMult)
		{
		}

		// Token: 0x040014D8 RID: 5336
		private static byte nextPrefix = 84;

		// Token: 0x040014D9 RID: 5337
		internal static readonly IList<ModPrefix> prefixes = new List<ModPrefix>();

		// Token: 0x040014DA RID: 5338
		internal static readonly IDictionary<PrefixCategory, IList<ModPrefix>> categoryPrefixes = new Dictionary<PrefixCategory, IList<ModPrefix>>();
	}
}
